<template>
  <div class="list-table noPad">
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('项目名称')" prop="dealNames">
        <el-input v-model="queryParams.dealNames" size="small" :placeholder="$t('项目名称')" />
      </el-form-item>
      <el-form-item :label="$t('年份')" prop="year">
        <DatePicker
          v-model="queryParams.year"
          size="small"
          type="year"
          :placeholder="$t('年份')"
        />
      </el-form-item>
      <el-form-item :label="$t('季度')" prop="quarter">
        <dictSelect
          v-model="queryParams.quarter"
          size="small"
          :clearable="true"
          dictionary="deal_report_period"
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:report:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="dealReportList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('项目名称')" prop="dealNames" min-width="30%" show-tooltip-when-overflow />
      <el-table-column :label="$t('报告类型')" align="center" prop="type" min-width="15%" :formatter="typeFormat" />
      <el-table-column :label="$t('年份')" align="center" prop="year" width="90" />
      <el-table-column :label="$t('季度')" align="center" prop="quarter" :formatter="quarterFormat" width="90" />
      <el-table-column :label="$t('数据起始时间')" align="center" prop="startTime" min-width="15%" width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('数据结束时间')" align="center" prop="endTime" min-width="15%" width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" min-width="15%" class-name="small-padding fixed-width" width="240">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['deal:report:edit']"
            size="small"
            type="text"
            icon="el-icon-download"
            @click="generateReport(scope.row)"
          >{{ $t('生成报告') }}</el-button>
          <el-button
            v-hasPermi="['deal:report:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['deal:report:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目报告对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealReportForm ref="dealReportForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listDealReport, delDealReport, exportDealReport, checkTemplate } from '@/api/deal/report/createReport/dealReport'
import dealReportForm from './dealReportForm.vue'
import { downLoadFileOpen } from '@/utils/zipdownload'

export default {
    components: {
      dealReportForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 项目报告表格数据
          dealReportList: [],
          // 弹出层标题
          title: '项目报告',
          // 是否显示弹出层
          formOpen: false,
          // 季度下拉框选项
          quarterOptions: [],
          // 季度下拉框选项
          typeOptions: [],
          // 查询参数
          queryParams: {
              year: undefined,
              quarter: undefined,
              startTime: undefined,
              endTime: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {
          }
      }
  },
  created() {
      this.getList()
      this.getDicts('deal_report_period').then(response => {
        this.quarterOptions = response.data
      })

      // 报告类型
      this.getDicts('deal_report_type').then(response => {
        this.typeOptions = response.data
      })
  },
  methods: {
    /** 查询项目报告列表 */
    getList() {
        this.loading = true
        listDealReport(this.queryParams).then(response => {
            this.dealReportList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 报告类型格式化
    typeFormat(row) {
      return this.selectDictLabel(this.typeOptions, row.type)
    },
    // 季度字典回显
    quarterFormat(row) {
      return this.selectDictLabel(this.quarterOptions, row.quarter)
    },
    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            year: undefined,
            quarter: undefined,
            startTime: undefined,
            endTime: undefined,
            createTime: undefined,
            createBy: undefined,
            updateTime: undefined,
            updateBy: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.dealReportForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.dealReportForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delDealReport(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        const queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return exportDealReport(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(function() {
        })
    },
    // 生成报告
    generateReport(row) {
       checkTemplate(row.id).then(res => {
         if (res.data) {
           downLoadFileOpen('/report/dealReport/generateReport?reportId=' + row.id)
         }
       })
    }
  }
}
</script>
